component  extends="ModelGlue.gesture.controller.Controller" output="false" beans="core"
{
	public ModelGlue.gesture.controller.Controller function init( required Any modelGlue){
		
		super.init( arguments.modelGlue );
		
		return this;
	}
	
	public ModelGlue.Core.Event function new( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		
		post.setIdPost( 0 );
		post.setStatus( 0 );
		
		arguments.event.setValue( 'Post', Post );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function save( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var login = beans.core.getService( 'Security' ).getLogin();
		var validator = service.getValidator();
		var category =beans.core.getService( 'Category' );
		
		
		post.setUuid( createUUID() );
		post.setPublishingdate( now() );
		Post.setAuthorId( login.getUser().getIdUser() );
		
		arguments.event.makeEventBean( Post );
		
		Post.setCategories( category.getCategoryList( arguments.event.getValue( "category" ), "idCategory" ) );
		
		arguments.event.setValue( 'Post', Post );
		
		validator.validate( Post );
			
		if( validator.hasErrors() ){
		
			arguments.event.setValue( 'error', validator.getErrors() );
			
			arguments.event.setValue( 'errorBox', 'Post not saved.' );
			
			arguments.event.addResult( 'save.failure' );
		
		}else{
			
				service.savePost( Post );

				arguments.event.setValue( 'successBox', 'Post saved.' );
				
				arguments.event.addResult( 'save.success' );
			
		}
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function delete( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var val = "";
		
		arguments.event.makeEventBean( Post );
		
		arguments.event.setValue( 'Post', Post );
		
		service.deletePost( Post );
		
		arguments.event.setValue( 'successBox', 'Post deleted.' );
									
		arguments.event.addResult( 'save.success' );
		
		return arguments.event;
	}	
	
	public ModelGlue.Core.Event function get( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var result = "";
		
		arguments.event.makeEventBean( Post );
		
		result = service.getPost( Post );
									
		arguments.event.setValue( 'Post', result );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function list( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var result = "";
		
		result = service.getPosts( Post );
									
		arguments.event.setValue( 'Post.list', result );
		
		return arguments.event;
	}
	
	/* blog front end */
	
	public ModelGlue.Core.Event function getPost( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var result = "";
		
		Post.setIdPost( arguments.event.getValue('idPost') );	
		
		result = service.getPost( Post );
		
		arguments.event.setValue( 'Post', result );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function getPostCount( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var result = "";

		result = service.getPostCount();
		
		arguments.event.setValue( 'Post.count', result );
		
		return arguments.event;
	}
	
	public ModelGlue.Core.Event function listArchive( required Any event ){
		
		var service = beans.core.getService( 'Post' );
		var Post = service.new();
		var result = "";
		
		// get all posts active
		Post.setStatus( 1 );
		
		result = service.getArchive( Post, arguments.event.getValue( 'page', 1 )  );
		
		arguments.event.setValue( 'prevPage', arguments.event.getValue( 'page', 1 ) - 1 );
		arguments.event.setValue( 'nextPage', arguments.event.getValue( 'page', 1 ) + 1 );
									
		arguments.event.setValue( 'Post.list', result );
		
		return arguments.event;
	}

}
